#! /usr/bin/env bash

ci_dir="$(dirname "${BASH_SOURCE[0]}")"

set -e

cd "${ci_dir}/.."

retval='1'

rm -f .clang-format
cp .clang-format.base .clang-format
for try in {1..10}; do
	errors="$(clang-format -dump-config 2>&1 >/dev/null)" || :
	if [ -z "${errors}" ]; then
		retval='0'

		break
	fi

	errors_line="$(echo "${errors}" | awk '
		/^YAML:/{
			sub(/^YAML:/, "");
			sub(/:.*/, "");
			print;
			exit;
		}
	')"

	if ! [ "${errors_line}" -gt -1 ] 2>/dev/null; then
		break
	fi

	rm -f .clang-format.new
	sed "${errors_line} d" .clang-format > .clang-format.new
	mv .clang-format.new .clang-format
done

exit "${retval}"
